<?php
//命令行自动创建文件

//生成文档格式：php sen touch class c_pdf/m_pdf
class touch
{
    public function __construct($data)
    {
        $class = $data['class'];
        if(!strpos($class, '_')) {
            echo '类名必须包含下划线';
            die;
        }
        $class_arr = explode('_', $class);
        $dir_class = $class_arr;
        array_pop($dir_class);

        $dir = DIR_Sen . '\\' . implode('\\', $dir_class);
        switch ($class_arr[0]) {
            case 'c':
                $content = "<?php\ndeclare(strict_types=1);\n\nclass " . $class . " extends Control\n{\n\tpublic function index()\n\t{\n\t\techo 'Hello $class!';\n\t}\n}";
                break;
            case 'm':
                $table_name = substr($class,2);
                $content = "<?php\ndeclare(strict_types=1);\n\nclass " . $class . " extends Model\n{\n\tprotected string \$table_name = '$table_name';\n}";
                break;
            default:
                echo '请输入c/m开头的类名';
                die;
        }
        $file_dir = str_replace('_', '\\', $class);
        if (!is_dir($dir)) mkdir($dir, 0755, true);
        $res = file_put_contents($file_dir . '.php', $content);
        if(!$res) {
            echo '写入文件失败';
            die;
        }
    }
}


